const app = getApp()
const request = require('../../../utils/request.js');
Page({
  data: {
    id: '',
    name: '',
    price: '',
    unit: '',
    quantity: '',
    origin: '',
    productionDate: '',
    shelfLife: '',
    storageCondition: '',
    transportMethod: '',
    description: '',
    images: [],
    phoneNumber: '',
    userId: '',
    userInfo: null
  },

  onLoad(options) {
    if (options.id) {
      this.setData({
        id: options.id
      })
      this.loadDetail()
    }
    this.setData({
      userInfo: app.getGlobalUserInfo()
    })
  },

  // 加载商品详情
  loadDetail() {
    wx.showLoading({
      title: '加载中...'
    })

    request.request({
      url: `${app.globalData.baseUrl}/api/residues/${this.data.id}`,
      method: 'GET',
      header: {
        'Authorization': `Bearer ${app.getGlobalToken()}`
      },
      success: (res) => {
        const data = res.data.data
        this.setData({
          ...data,
          images: data.images.map((item) => app.globalData.baseUrl + item) || [app.globalData.baseUrl + data.image],
        })
        if(data.userId != null) {
          request.request({
            url: `${app.globalData.baseUrl}/api/user/${data.userId}`,
            method: 'GET',
            header: {
              'Authorization': `Bearer ${app.getGlobalToken()}`
            },
            success:(res)=>{
              const user = res.data.data
              this.setData({
                phoneNumber: user.phone,
              })
            }
          })
        }
      },
      fail: (err) => {
        wx.showToast({
          title: '加载失败',
          icon: 'none'
        })
        console.error('加载商品详情失败:', err)
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },

  // 立即购买
  buyNow() {
    // 检查是否登录
    if (!app.getGlobalToken()) {
      wx.showModal({
        title: '提示',
        content: '请先登录后再购买',
        confirmText: '去登录',
        success: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login'
            })
          }
        }
      })
      return
    }

    // 跳转到订单确认页面
    wx.navigateTo({
      url: `/pages/orders/confirm/confirm?id=${this.data.id}`
    })
  },

  // 联系卖家
  contactSupplier() {
    if (!this.data.phoneNumber) {
      wx.showToast({
        title: '暂无联系方式',
        icon: 'none'
      })
      return
    }

    wx.makePhoneCall({
      phoneNumber: this.data.phoneNumber,
      fail: () => {
        wx.showToast({
          title: '拨打电话失败',
          icon: 'none'
        })
      }
    })
  },

  // 收藏/取消收藏
  toggleFavorite() {
    const action = this.data.isFavorite ? 'unfavorite' : 'favorite'
    
    request.request({
      url: `${app.globalData.baseUrl}/api/residues/${this.data.id}/${action}`,
      method: 'POST',
      header: {
        'Authorization': `Bearer ${app.getGlobalToken()}`
      },
      success: () => {
        this.setData({
          isFavorite: !this.data.isFavorite
        })
        wx.showToast({
          title: this.data.isFavorite ? '收藏成功' : '已取消收藏',
          icon: 'success'
        })
      },
      fail: (err) => {
        wx.showToast({
          title: '操作失败',
          icon: 'none'
        })
        console.error('收藏操作失败:', err)
      }
    })
  }
}) 